#include "vme_sis.h"

void readA16D32(int handle, u_int16_t addr, u_int32_t *dest)
{
		int r;

		if( (r=vme_A16D32_read(handle,addr, dest)) != 0 ) {

				stringstream ss;
				
				ss << hex << "readA16D32(): "
					 << "vme_A16D32_read() returned non zero"
					 << ", handle=0x" << handle
					 << ", address=0x" << addr
					 << ", data_address=0x" << dest
					 << dec;

				throw IOError(ss.str().c_str(),r);
		}

}

void readA32D16(int handle, u_int32_t addr, u_int16_t *dest)
{
		int r;

		if( (r=vme_A32D16_read(handle,addr, dest)) != 0 ) {
				stringstream ss;
				
				ss << hex << "readA32D16(): "
					 << "vme_A32D16_read() returned non zero"
					 << ", handle=0x" << handle
					 << ", address=0x" << addr
					 << ", data_address=0x" << dest
					 << dec;

				throw IOError(ss.str().c_str(),r);
		}

}

void readA32D32(int handle, u_int32_t addr, u_int32_t *dest)
{
		int r;

		if( (r=vme_A32D32_read(handle,addr, dest)) != 0 ) {
				stringstream ss;
				
				ss << hex << "readA16D32(): "
					 << "vme_A32D32_read() returned non zero"
					 << ", handle=0x" << handle
					 << ", address=0x" << addr
					 << ", data_address=0x" << dest
					 << dec;

				throw IOError(ss.str().c_str(),r);
		}

}

void readA16D32DMA(int handle,u_int16_t addr,u_int32_t *buf,u_int32_t count)
{
		stringstream ss;
		u_int32_t read;
		int r;

    if( (r=vme_A16DMA_D32FIFO_read(handle,addr,buf,count,&read)) != 0 ) {

				ss << hex
					 << "readA16D32DMA():"
					 << " vme_A16DMA_D32FIFO_read() returned non zero"
					 << ",  parameters: handle=0x" << handle
					 << ", vme_address=0x" << addr
					 << ", buffer_adress=0x" << (u_int32_t)buf
					 << ", count=0x" << count
					 << ", read=0x" << read
					 << dec << endl;
				
				throw IOError(ss.str().c_str(),r);
		}

		if(read!=count) {
				ss << "readA16D32DMA():"
					 << " vme_A16DMA_D32FIFO_read() returned " << read
					 << "bytes - requested were " << count 
					 << endl;
				
				throw IOError(ss.str().c_str(),r);
		}
}

void readA32D32DMA(int handle,u_int32_t addr,u_int32_t *buf,u_int32_t count)
{
		stringstream ss;
		u_int32_t read;
		int r;

    if( (r=vme_A32DMA_D32FIFO_read(handle,addr,buf,count,&read)) != 0 ) {

				ss << hex
					 << "readA32D32DMA():"
					 << " vme_A32DMA_D32FIFO_read() returned non zero"
					 << ",  parameters: handle=0x" << handle
					 << ", vme_address=0x" << addr
					 << ", buffer_adress=0x" << (u_int32_t)buf
					 << ", count=0x" << count
					 << ", read=0x" << read
					 << dec << endl;
				
				throw IOError(ss.str().c_str(),r);
		}

		if(read!=count) {
				ss << "readA32D32DMA():"
					 << " vme_A32DMA_D32FIFO_read() returned " << read
					 << "bytes - requested were " << count 
					 << endl;
				
				throw IOError(ss.str().c_str(),r);
		}
}

void writeA32D16(int handle,u_int32_t addr,u_int16_t d)
{
    int r;

    if( (r=vme_A32D16_write(handle,addr,d) != 0) ) {
				stringstream ss;

				ss << hex
					 << "writeA32D16(): vme_A32D16_write() returned non zero"
					 << ",  parameters: handle=0x" << handle
					 << ", vme_address=0x" << addr
					 << ", data=0x" << d
					 << dec << endl;
				
				throw IOError(ss.str().c_str(),r);
		}
}
